package cn.wss.flowable.config;

import cn.wss.flowable.service.FlowableHistoryService;
import cn.wss.flowable.service.FlowableRepositoryService;
import cn.wss.flowable.service.FlowableRuntimeService;
import cn.wss.flowable.service.FlowableTaskService;
import cn.wss.flowable.service.impl.FlowableHistoryServiceImpl;
import cn.wss.flowable.service.impl.FlowableRepositoryServiceImpl;
import cn.wss.flowable.service.impl.FlowableRuntimeServiceImpl;
import cn.wss.flowable.service.impl.FlowableTaskServiceImpl;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class FlowableConfiguration implements
    EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {

  @Bean
  @ConditionalOnBean(DataSource.class)
  public ProcessEngine processEngine(DataSource dataSource) {
    ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration();
    cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
    cfg.setDataSource(dataSource);
    return cfg.buildProcessEngine();
  }

  @Override
  public void configure(SpringProcessEngineConfiguration engineConfiguration) {
    engineConfiguration.setActivityFontName("宋体");
    engineConfiguration.setLabelFontName("宋体");
    engineConfiguration.setAnnotationFontName("宋体");
  }

  @Bean
  public FlowableHistoryService flowableHistoryService() {
    return new FlowableHistoryServiceImpl();
  }

  @Bean
  public FlowableRepositoryService flowableRepositoryService() {
    return new FlowableRepositoryServiceImpl();
  }

  @Bean
  public FlowableRuntimeService flowableRuntimeService() {
    return new FlowableRuntimeServiceImpl();
  }

  @Bean
  public FlowableTaskService flowableTaskService() {
    return new FlowableTaskServiceImpl();
  }
}
